/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.gui.documents;

import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Writer;

import javax.swing.ImageIcon;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JXTextPane;
import javax.swingx.CodeEditor;

import ui.gui.documents.actions.ActionCopy;
import ui.gui.documents.actions.ActionCut;
import ui.gui.documents.actions.ActionPaste;
import ui.gui.documents.actions.ActionRedo;
import ui.gui.documents.actions.ActionUndo;
import ui.gui.documents.actions.ActionFormatCode;
import ui.gui.documents.actions.ActionViewAltaricaTree;

/**
 *
 * @author stephane
 */
public class PlainTextEditor extends AbstractDocument {

    private static final long serialVersionUID = 1L;
    private JTextPane _editor = new JTextPane();
    private JScrollPane _scrollPane = new JScrollPane();
    private BorderLayout _layout = new BorderLayout();
    private JPopupMenu _popupMenu = new JPopupMenu();

    
    @Override
    public void initComponents() {
    	
    }
    
    public PlainTextEditor() {
    	this.setExtension(".txt");
    	this.setFileDescription("Plain text file");
        this.setLayout(this._layout);
        this.add(_scrollPane, BorderLayout.CENTER);
        this._scrollPane.setViewportView(_editor);
        this._editor.addKeyListener(new KeyAdapter() {
        	@Override
        	public void keyTyped(KeyEvent e) {
        		super.keyTyped(e);
        		setIsDirty(true);
        	}
        });
        this._editor.addMouseListener(new MouseAdapter() {
        	@Override
        	public void mouseClicked(MouseEvent e) {
        		super.mouseClicked(e);
        		if (e.getButton() == MouseEvent.BUTTON3) {
        			_popupMenu.show(_editor, e.getX(), e.getY());
        		}
        	}
        });
        
        addAction(new ActionFormatCode(this));
        addAction(null);
        addAction(new ActionUndo(this));
        addAction(new ActionRedo(this));
        addAction(null);
        addAction(new ActionCopy(this));
        addAction(new ActionPaste(this));
        addAction(new ActionCut(this));
        
        
        for (EditorAction action : getAvailableActions()) {
        	if (action != null) {
        	_popupMenu.add(action);
        	}
        	else {
        		_popupMenu.addSeparator();
        	}
        }
    }

    @Override
    public void OnLoad() throws Exception {
        _editor.setText("");
        StringBuilder builder = new StringBuilder();
        BufferedReader inputFile = new BufferedReader(new FileReader(this.getFile()));

        String line = inputFile.readLine();
        while (line != null) {
            builder.append(line);
            builder.append(System.getProperty("line.separator"));
            line = inputFile.readLine();
        }
        _editor.setText(builder.toString());
        if (inputFile != null) {
            inputFile.close();
        }
    }

    @Override
    public void OnSave() throws Exception {
    	try
    	{
	        Writer output = new BufferedWriter(new FileWriter(this.getFile()));
	        output.write(this._editor.getText());
	        output.close();
	        setIsDirty(false);
	        common.Commands.RunCommand("load "+ this.getFile().getAbsolutePath(), true);
    	}
    	catch (Exception ex) {
    		throw ex;
    	}
    }

    public void gotoLine(int line) {
    	line--;
    	int currentLine = 0;
        int currentSelection = 0;
        String textContent = _editor.getText();
        String seperator = System.getProperty("line.separator");
        int seperatorLength = seperator.length();
        while (currentLine < line) {
            int next = textContent.indexOf(seperator,currentSelection);
            if (next > -1) {
                currentSelection = next + seperatorLength;
                currentLine++;
            } else {
                currentSelection = textContent.length();
                currentLine= line; 
            }
        }
        _editor.setCaretPosition(currentSelection);
    }


    @Override
    public ImageIcon getIcon() {
        return new javax.swing.ImageIcon(getClass().getResource("/resources/altaricaIcon.png"));
    }
    
    
    public String getText() {
        return _editor.getText();
    }
    
    public void setText(String text) {
        this._editor.setText(text);
    }
    
}